package sg.edu.nus.iss.pacman.form;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 * Screen for user to select available remote devices
 * 
 * @author Daniel
 *
 */
public class SelectRemoteDeviceList extends List implements CommandListener {

	private static final String TITLE = "Select Available Remote Devices";
	private PacManMIDlet mainApp;
	
	private String[] listRemoteDevices;
	
	private Command cmdSelect;
	private Command cmdBack;
	
	/**
	 * Constructor for this screen
	 * 
	 * @param mainApp PacManMIDlet
	 * @param listRemoteDevices list of remote devices
	 */
	public SelectRemoteDeviceList(PacManMIDlet mainApp, String[] listRemoteDevices) {
		super(TITLE, List.IMPLICIT, listRemoteDevices, null);
		this.mainApp = mainApp;
		this.listRemoteDevices = listRemoteDevices;
		init();
	}
	
	/**
	 * Initializes the screen
	 */
	private void init() {		
		cmdSelect = new Command(PacManMIDlet.LABEL_CMD_SELECT, Command.OK, 1);
		cmdBack = new Command(PacManMIDlet.LABEL_CMD_BACK, Command.EXIT, 1);
		
		addCommand(cmdSelect);
		addCommand(cmdBack);
		setCommandListener(this);
	}

	/**
	 * This method is used to handle command actions
	 */
	public void commandAction(Command cmd, Displayable disp) {
		if (cmd == cmdBack) {
			mainApp.displaySendChallengeChoiceList();
		} else if (cmd == cmdSelect) {
			int idx = getSelectedIndex();
			
			if (idx >= 0) {
				mainApp.sendChallengeViaBluetooth(idx, listRemoteDevices[idx]);	
			}
		}
	}

}
